package cn.yudao.filter;

import com.alibaba.fastjson.JSON;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@Component
public class CustomFilter extends ZuulFilter {

    /**
    * @Title: filterType
    * @Description: 拦截器类型 "pre","post"
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/31 11:10
    * @Parameters:
    * @Return
    */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
    * @Title: filterOrder
    * @Description: 执行优先级
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/31 11:10
    * @Parameters:
    * @Return
    */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
    * @Title: shouldFilter
    * @Description: 是否执行拦截
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/31 11:10
    * @Parameters:
    * @Return
    */
    @Override
    public boolean shouldFilter() {
        /**
        判断是否进入的公共的url
         */
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest(); // 获取请求体
        String uri = request.getRequestURI();
        // 判断当前的访问地址是不是访问注册和登录或者静态资源的
        return uriIsTrue(uri);
    }

    /**
    * @Title: uriIsTrue
    * @Description: 校验访问的路径需要放行
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/31 13:58
    * @Parameters:
    * @Return
    */
    private boolean uriIsTrue(String uri){
        if (uri.endsWith("/register") || uri.endsWith("/login") || uri.contains("static")){
            return false;
        }
        return true;
    }

    /**
    * @Title: run
    * @Description: 拦截的代码
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/31 11:10
    * @Parameters:
    * @Return
    */
    @Override
    public Object run() throws ZuulException {
        // 获取请求体
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        // 判断token是否存在
        String token = request.getHeader("token");
        if (StringUtils.isBlank(token) || !tokenIsTrue(token)){
            HashMap<String, Object> map = new HashMap<>();
            map.put("success",false);
            map.put("message","nologin");
            map.put("code",402);
            map.put("data","状态错误");
            // 将map转换为json字符串
            String json = JSON.toJSONString(map);

            // 获取响应对象
            HttpServletResponse response = RequestContext.getCurrentContext().getResponse();
            response.setStatus(402);
            try {
                response.getWriter().write(json);
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 阻止filter继续往后执行(固定写法)
            RequestContext.getCurrentContext().setSendZuulResponse(false);
        }


        return null;
    }

    private boolean tokenIsTrue(String token){
        // TODO 对token的值进行业务校验
        return true;
    }
}
